package com.cql.web.controller.business;

import com.cql.common.constant.FoodCircleConstant;
import com.cql.common.core.base.AjaxResult;
import com.cql.common.core.base.BaseController;
import com.cql.common.core.security.LoginBody;
import com.cql.common.core.security.LoginUser;
import com.cql.framework.config.security.service.LoginService;
import com.cql.framework.config.security.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author cql
 */
@RestController
public class LoginController extends BaseController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private TokenService tokenService;

    /**
     * TODO 登录（防止重复登录：好像一些门户网站允许你多方登录）
     *
     * @param loginBody
     * @return
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        String token = loginService.login(loginBody.getAccount(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        ajax.put(FoodCircleConstant.TOKEN, token);
        return ajax;
    }

    /**
     * 获取登录用户的信息
     *
     * @return
     */
    @GetMapping("/userinfo")
    public AjaxResult getInfo(HttpServletRequest request) {
        AjaxResult ajax = AjaxResult.success();
        /**
         * 1.基本信息
         * 2.角色
         * 3.权限
         */
        LoginUser loginUser = tokenService.getLoginUser(request);

        ajax.put("user", loginUser.getUserDO());
        ajax.put("roles", loginUser.getRoles());
        ajax.put("permissions", loginUser.getPermissions());

        return ajax;
    }

}
